var app = require("http").createServer()
var io = require("socket.io")(app)

var PORT = 8080

var clientCount = 0

app.listen(PORT)

io.on("connection", function (socket) {
  clientCount++
  socket.nickname = '用户' + clientCount
  io.emit("enter", socket.nickname + " 进入聊天室了")

  socket.on("message", function (str) {
    io.emit("message", socket.nickname + "：" + str)
  })
  socket.on("disconnect", function () {
    io.emit("leave", socket.nickname + " 离开聊天室了")
  })
})

console.log("websocket server listening on port：" + PORT)